from sqlmodel import Field
from typing import Optional
from models.base_model import BaseModel, SQLModel
from sqlalchemy import Column, TEXT
from datetime import datetime


class operLogBase(SQLModel):

    title: str = Field(nullable=True, max_length=255, description="模块标题")
    business_type: int = Field(nullable=True, description="业务类型")
    user_id: int = Field(default=None, description="用户id")
    username: str = Field(nullable=True, max_length=24, description="用户名")
    method: str = Field(nullable=True, max_length=20, description="请求方法")
    path: str = Field(nullable=True, max_length=50, description="请求路径")
    ip: Optional[str] = Field(nullable=True, max_length=50, description="请求IP")
    args: Optional[str] = Field(default=None, sa_column=Column(TEXT), description="请求参数")
    message: Optional[str] = Field(default=None, sa_column=Column(TEXT), description="日志信息")


class operLog(BaseModel, operLogBase, table=True):
    """
    操作日志
    """
    __tablename__ = 't_oper_log'


class loginLogBase(SQLModel):
    username: str = Field(nullable=True, max_length=24, description="用户名")
    ip_address: Optional[str] = Field(nullable=True, max_length=50, description="IP地址")
    login_location: Optional[str] = Field(nullable=True, max_length=100, description="登录地址")
    browser: str = Field(nullable=True, max_length=50, description="浏览器类型")
    os: str = Field(nullable=True, max_length=50, description="操作系统")
    status: bool = Field(default=False, description="登录状态（true成功 false失败）")
    msg: Optional[str] = Field(max_length=255, description="提示消息")
    login_time: datetime = Field(default_factory=datetime.now, nullable=False, description="登录时间")


class loginLog(BaseModel, loginLogBase, table=True):
    """
    登录日志
    """

    __tablename__ = 't_login_log'
